package com.tangcco.lockwell.controller.admin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.Message;
import com.tangcco.lockwell.service.MessageService;
import com.tangcco.lockwell.utility.DateToString;
import com.tangcco.lockwell.viewmodel.admin.message.MessageAddRequestVM;
import com.tangcco.lockwell.viewmodel.admin.message.MessagePageRequestVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserPageRequestVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("AdminMessageController")
@RequestMapping("/api/admin/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @Autowired
    private WebContext webContext;


    @RequestMapping("select")
    public RestResponse<PageInfo<Message>> findAll(@RequestBody MessagePageRequestVM messagePageRequestVM) {
        PageInfo<Message> pageInfo = messageService.selectPage(messagePageRequestVM);
        return RestResponse.ok(pageInfo);
    }
    @RequestMapping("delete/{messageid}")
    public RestResponse<Message> deleteById(@PathVariable("messageid") Integer messageid) {
        int deletelikes = messageService.deletelikes(messageid);
        return RestResponse.ok();
    }

    //查询回复
    @GetMapping("datail")
    public RestResponse<Map<String,Object>> findByPid(Integer id) {
        List<Message> messageList = messageService.findByPid(id);
        Message message=messageService.findById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("message",message);
        map.put("children",messageList);
        return RestResponse.ok(map);
    }

    //添加回复
    @PostMapping("/add/reply")
    public RestResponse<String> addReply(@RequestBody MessageAddRequestVM messageAddRequestVM){
        //根据Id查询Message
        Message message = messageService.findById(messageAddRequestVM.getId());
        //封装回复对象
        Message reply=new Message();
        reply.setBelongs(message.getMessageId().toString());
        reply.setClassify(1);
        reply.setLikes(0);
        reply.setPublishContent(messageAddRequestVM.getPublishContent());
        reply.setStatus(1);
        reply.setSubjectId(message.getSubjectId());
        reply.setUserId(webContext.getCurrentUser().getUserId());
        reply.setTime(DateToString.dateToString(new Date()));
        reply.setCreateTime(DateToString.dateToString(new Date()));
        //添加到数据库
        messageService.insertReply(reply);

        //返回结果
        return RestResponse.ok("回复成功");
    }
}
